//
//  JTMeViewController.m
//  百思不得姐
//
//  Created by sun lei on 16/11/17.
//  Copyright © 2016年 BeiJingWangDianJieTong. All rights reserved.
//

#import "JTMeViewController.h"
#import "JTSettingViewController.h"
#import "JTMeTableViewCell.h"
#import "JTMeFooterView.h"

@interface JTMeViewController ()

@end

@implementation JTMeViewController

- (instancetype)init {
    
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNavigationBar];
    
    [self setupTableView];
}

- (void)setupNavigationBar {
    
    // 标题
    [self.navigationItem setTitle:@"我的"];
    // 右边-设置
    UIBarButtonItem *settingItem = [UIBarButtonItem jt_itemWithImage:@"mine-setting-icon" highLightedImage:@"mine-setting-icon-click" target:self action:@selector(settingClick:)];
    // 右边-月亮
    UIBarButtonItem *moonItem = [UIBarButtonItem jt_itemWithImage:@"mine-moon-icon" highLightedImage:@"mine-moon-icon-click" target:self action:@selector(moonClick:)];
    [self.navigationItem setRightBarButtonItems:@[settingItem, moonItem]];
}

- (void)setupTableView {
    
    [self.tableView setBackgroundColor:JTCommonBgColor];
    [self.tableView setSectionHeaderHeight:0];
    [self.tableView setSectionFooterHeight:JTMargin];
    [self.tableView setContentInset:UIEdgeInsetsMake(JTMargin - 35, 0, 0, 0)];
    
    // 设置footer
    JTMeFooterView *footerView = [[JTMeFooterView alloc] init];
    [self.tableView setTableFooterView:footerView];
}

- (void)settingClick:(UIButton *)sender {
    
    JTLogFunc
    
    JTSettingViewController *test = [[JTSettingViewController alloc] init];
    [self.navigationController pushViewController:test animated:YES];
}

- (void)moonClick:(UIButton *)sender {
    
    JTLogFunc
}

#pragma mark - <UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 1.确定重用标识
    static NSString *const ID = @"JTMeTableViewCell";
    // 2.从缓存池中取
    JTMeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    // 3.如果为空就手动创建
    if (!cell) {
        
        cell = [[JTMeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    // 4.设置数据
    if (indexPath.section == 0) {
        
        [cell.textLabel setText:@"登录/注册"];
        [cell.imageView setImage:[UIImage imageNamed:@"setup-head-default"]];
    }
    else if (indexPath.section == 1) {
        
        [cell.textLabel setText:@"离线下载"];
        [cell.imageView setImage:nil]; // 只要有其他cell设置过imageView.image，其他不显示图片的cell都需要设置imageView.image = nil，防止循环利用
    }
    
    return cell;
}

#pragma mark - <UITableViewDelegate>
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
